package gq.example.controller;


import gq.example.result.Result;
import gq.example.utils.AliOssUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;


@Tag(name = "文件上传")
@RestController
@RequestMapping("/file")
@Slf4j
public class FileController {
    @Resource
    private AliOssUtil aliOssUtil;

    /**
     * 文件上传
     * @param file
     * @return
     * @throws IOException
     */
    @Operation(summary = "文件上传")
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws IOException {
        //获取文件原始名
        String originalFilename = file.getOriginalFilename();

        //获取文件后缀
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));

        //文件重命名
        String newFileName = UUID.randomUUID().toString() + suffix;  //采用UUID命名

        //获取文件路径
        String FilePath = aliOssUtil.upload(file.getBytes(), newFileName);

        return Result.success(FilePath);
    }
}



